'use strict';

var express = require('express');
var router = express.Router();

var $file = require('../midware/file');
var $homework = require('../midware/homework');
var $user = require('../midware/admin/user');
var $session = require('../midware/session');

let v = (v, def)=>v === undefined ? def : v;

//获取作业列表
router.get('/list', function (req, res, next) {

    let ssid = req.cookies.ssid;

    new Promise((O, X)=> {
        O();
    })
        .then(()=> {
            $session.isAdmin(ssid);
            return $homework.getList();
        })
        .then((homeworks)=> {
            res.json({
                code: 0,
                list: homeworks
            });
        })
        .catch((e)=> {
            res.json({
                code: e.code,
                msg: e.msg
            });
        });
});

//获取某次作业详情
//同时还要获取需要交作业的学生列表
router.get('/', function (req, res, next) {

    let id = req.query._id;
    let ssid = req.cookies.ssid;

    let _homework_ = null;

    try {
        $session.isAdmin(ssid);
        $homework
            .getOnebyId(id)
            .then((homework)=> {
                _homework_ = homework;
                return $user.getUsersByClass(homework.class)
            })
            .then((students)=> {
                res.json({
                    code: 0,
                    info: _homework_,
                    students: students
                });
            })
            .catch((e)=> {
                console.log(e);
                res.json({
                    code: 9999,
                    msg: '接口发生错误'
                });
            })
    } catch (e) {
        res.json({
            code: 9999,
            msg: e.msg || '接口发生错误'
        });
    }

});

//新建作业
router.post('/', function (req, res, next) {

    try {

        let ssid = req.cookies.ssid;

        let data = JSON.parse(req.body.json);

        let _class = v(data.class),
            title = v(data.title),
            description = v(data.description),
            overtime = v(data.overtime);

        new Promise((O, X)=> {
            O();
        })
            .then(()=> {
                $session.isAdmin(ssid);

                if (!_class || !_class.length || _class.length == 0) {
                    throw {
                        code: 9999,
                        msg: '没有选择班级'
                    };
                }
                if (!title || !title.length || title.length == 0) {
                    throw {
                        code: 9999,
                        msg: '没有标题'
                    };
                }
                if (!overtime || overtime == 0) {
                    throw {
                        code: 9999,
                        msg: '没有截止时间'
                    };
                }

                return $homework.save({
                    class: _class,
                    title: title,
                    overtime: overtime,
                    description: description
                })
            })
            .then(()=> {
                res.json({
                    code: 0
                });
            })
            .catch((e)=> {
                console.log(e);
                res.json({
                    code: e.code || 9999,
                    msg: e.msg || '未知错误'
                });

            });

    } catch (e) {
        res.json({
            code: 9999,
            msg: '接口发生错误'
        });
    }

});

//编辑作业
router.put('/', function (req, res, next) {

    try {

        let ssid = req.cookies.ssid;

        let data = JSON.parse(req.body.json);

        let id = v(data._id),
            _class = v(data.class),
            title = v(data.title),
            description = v(data.description),
            overtime = v(data.overtime);

        new Promise((O, X)=> {
            O();
        })
            .then(()=> {
                $session.isAdmin(ssid);

                if (!_class || !_class.length || _class.length == 0) {
                    throw {
                        code: 9999,
                        msg: '没有选择班级'
                    };
                }
                if (!title || !title.length || title.length == 0) {
                    throw {
                        code: 9999,
                        msg: '没有标题'
                    };
                }
                if (!overtime || overtime == 0) {
                    throw {
                        code: 9999,
                        msg: '没有截止时间'
                    };
                }

                return $homework.update(id, {
                    class: _class,
                    title: title,
                    description: description,
                    overtime: overtime
                });
            })
            .then(()=> {
                res.json({
                    code: 0
                });
            })
            .catch((e)=> {
                console.log(e);

                res.json({
                    code: e.code,
                    msg: e.msg
                });

            });

    } catch (e) {
        res.json({
            code: 9999,
            msg: '接口发生错误'
        });
    }

});

//删除作业
router.delete('/', function (req, res, next) {

    try {

        let cookies = req.cookies;
        let ssid = cookies.ssid;

        let id = v(req.body._id);

        new Promise((O, X)=> {
            O();
        })
            .then(()=> {
                $session.isAdmin(ssid);

                return $homework.getOnebyId(id);
            })
            .then((homework)=> {

                let fileList = [].concat(homework.files);

                return Promise.all(fileList.map((file)=> {
                    return $file.deleteByPath(file.path)
                }));
            })
            .then(()=> {
                return $homework.deleteById(id)
            })
            .then(()=> {
                res.json({
                    code: 0
                });
            })
            .catch((e)=> {
                console.log(e);

                res.json({
                    code: e.code,
                    msg: e.msg
                });

            });

    } catch (e) {
        console.log('ERROR:' + e);
        res.json({
            code: 9999,
            msg: '接口发生错误'
        });
    }

});

module.exports = router;